package cn.fl.mcr.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrWriteOffConfirmDetailService;
import cn.fl.mcr.transform.McrWriteOffConfirmDetailVOTransform;
import cn.fl.mcr.vo.McrWriteOffConfirmDetailDVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.dto.McrWriteOffConfirmDetailDTO;
import cn.lionbridgecapital.loan.model.po.McrWriteOffConfirmDetail;
import cn.lionbridgecapital.loan.model.vo.McrWriteOffConfirmDetailVO;
import cn.lionbridgecapital.loan.service.api.IMcrWriteOffConfirmDetailApiService;

/**
 * @Description: 核销确认明细 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrWriteOffConfirmDetailServiceImpl implements IMcrWriteOffConfirmDetailService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrWriteOffConfirmDetailService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrWriteOffConfirmDetailApiService mcrWriteOffConfirmDetailApiService;

	@Override
	public McrWriteOffConfirmDetailVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrWriteOffConfirmDetailDTO dto = null;//this.自定义ApiService.selectById(id);
		return McrWriteOffConfirmDetailVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrWriteOffConfirmDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.mcrWriteOffConfirmDetailApiService.add(McrWriteOffConfirmDetailVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffConfirmDetailApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrWriteOffConfirmDetailVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffConfirmDetailApiService.modify(McrWriteOffConfirmDetailVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = mcrWriteOffConfirmDetailApiService.searchMcrWriteOffConfirmDetailListPage(page);
		BaseLogUtils.info(logger, "McrWriteOffConfirmDetailServiceImpl.searchListPage", "核销明细返回:"+JSON.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		List<McrWriteOffConfirmDetailDTO> resultList = (List<McrWriteOffConfirmDetailDTO>)page.getResultList();
		List<McrWriteOffConfirmDetailDVO> voList = Lists.newArrayList();
        DecimalFormat df = new DecimalFormat("######0.00");
		if(CollectionUtils.isNotEmpty(resultList)) {
			for(McrWriteOffConfirmDetailDTO detail:resultList) {
				McrWriteOffConfirmDetailDVO vo = new McrWriteOffConfirmDetailDVO();
				vo.setBatNo(detail.getBatNo());
				vo.setCstNm(detail.getCstNm());
				vo.setRepayDate(detail.getRepayDate());
				vo.setRepayType(detail.getRepayType());
				vo.setPerdNo(detail.getPerdNo());
				vo.setPactNo(detail.getPactNo());
				
				if( null != detail.getRepayAmt()) {
					vo.setRepayAmt(df.format(DecimalUtils.convertFenToYuan(detail.getRepayAmt())));
				}else {
					vo.setRepayAmt("0.00");
				}
				if(null != detail.getRepayPrincipal()) {
					vo.setRepayPrincipal(df.format(DecimalUtils.convertFenToYuan(detail.getRepayPrincipal())));
				}else {
					vo.setRepayPrincipal("0.00");
				}
				if(null != detail.getRepayInterest()) {
					vo.setRepayInterest(df.format(DecimalUtils.convertFenToYuan(detail.getRepayInterest())));
				}else {
					vo.setRepayInterest("0.00");
				}
				if(null != detail.getRepayPenaltyInterest()) {
					vo.setRepayPenaltyInterest(df.format(DecimalUtils.convertFenToYuan(detail.getRepayPenaltyInterest())));
				}else {
					vo.setRepayPenaltyInterest("0.00");
				}
	        	voList.add(vo);
			}
			page.setResultList(voList);
		}
		BaseLogUtils.info(logger, "McrWriteOffConfirmDetailServiceImpl.searchListPage", "核销明细返回--->:"+JSON.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return page;
	}
}
